package org.example.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

/**
 * @author hh
 * @since 2024/10/27
 */
public class TestByteBuf {
    public static void main(String[] args) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer(9, 100);
        System.out.println("分配ByteBuf初始容量为9，最大容量为100");
        byteBuf.writeBytes(new byte[]{1,2,3,4});
        System.out.println("写入4个字节（1,2,3,4）");
        getByteBuf(byteBuf);
        readByteBuf(byteBuf);
    }

    private static void readByteBuf(ByteBuf byteBuf) {
        while (byteBuf.isReadable()) {
            System.out.println("取出一个字节：" + byteBuf.readByte());
        }
    }

    private static void getByteBuf(ByteBuf byteBuf) {
        for (int i = 0; i < byteBuf.readableBytes(); i++) {
            // 使用get方法取数据不会影响ByteBuf指针属性值
            System.out.println("读一个字节：" + byteBuf.getByte(i));
        }
    }
}
